﻿using CJCMS.Contracts;
using CJCMS.Contracts.DTO.Product;
using CJCMS.Contracts.Service;
using CJCMS.Domain;
using CJCMS.Domain.Entity;
using CJCMS.Domain.Service;
using CJCMS.Framework.Excel;
using CJCMS.Framework.GlobalInfo;
using CJCMS.Framework.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CJCMS.Application
{
    public sealed class ProductManager : IProductManager
    {
        /// <summary>
        /// 添加商品
        /// </summary>
        /// <param name="p"></param>
        public void AddProduct(ProductDTO p)
        {
            TValidationHelper<ProductDTO>.TValidation(p);
            ProductService service = new ProductService();

            service.AddProduct(AutoMapper.Mapper.Map<ProductDTO, Product>(p));
        }

        /// <summary>
        /// 修改商品
        /// </summary>
        /// <param name="p"></param>
        public void UpdateProduct(ProductInfo p)
        {
            TValidationHelper<ProductInfo>.TValidation(p);
            ProductService service = new ProductService();

            service.UpdateProduct(AutoMapper.Mapper.Map<ProductInfo, Product>(p));
        }

        /// <summary>
        /// 设置商品上线
        /// </summary>
        /// <param name="p"></param>
        public void SetProductOn(ProductStatusDTO p)
        {
            TValidationHelper<ProductStatusDTO>.TValidation(p);
            ProductService service = new ProductService();

            service.SetProductOn(p.Id);
        }

        /// <summary>
        /// 设置商品下线
        /// </summary>
        /// <param name="p"></param>
        public void SetProductOff(ProductStatusDTO p)
        {
            TValidationHelper<ProductStatusDTO>.TValidation(p);
            ProductService service = new ProductService();

            service.SetProductOff(p.Id);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="categoryId">分类编号</param>
        /// <param name="index">页码</param>
        /// <param name="pagecount">页大小</param>
        /// <param name="totalCount">总数</param>
        /// <returns></returns>
        public IList<ProductInfo> FetchProductByCategory(string categoryId, int index, int pagecount, out int totalCount)
        {
            ProductService service = new ProductService();

            return AutoMapper.Mapper.Map<IList<Product>, IList<ProductInfo>>(service.FetchAllByCategory(categoryId, index, pagecount, out totalCount));
        }

        /// <summary>
        /// 按照商品名称模糊查询
        /// </summary>
        /// <param name="categoryId">分类编号</param>
        /// <param name="name">商品名称</param>
        /// <param name="index">页码</param>
        /// <param name="pagecount">页大小</param>
        /// <param name="totalCount">总数</param>
        /// <returns></returns>
        public IList<ProductInfo> FetchProductByCategoryAndName(string categoryId, string name, int index, int pagecount, out int totalCount)
        {
            ProductService service = new ProductService();

            return AutoMapper.Mapper.Map<IList<Product>, IList<ProductInfo>>(service.FetchAllByCategoryAndName(categoryId,name, index, pagecount, out totalCount));
        }

        /// <summary>
        /// 根据商品编号获取商品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ProductInfo GetOneById(string id)
        {
            ProductService service = new ProductService();

            return AutoMapper.Mapper.Map<Product, ProductInfo>(service.GetOneById(id));

        }
        /// <summary>
        /// 从Excel导入商品到数据库
        /// </summary>
        public void ImportProductFromExcel(string fileName)
        {
            ExcelHelper<ProductForExcel> excelHelper = new ExcelHelper<ProductForExcel>(GlobalHelper.GetValueByKey("ExcelFile") + fileName + ".xls",string.Empty);
            int i = 4;
            while (true)
            {
                try
                {
                    Product p = AutoMapper.Mapper.Map<ProductForExcel, Product>(excelHelper.ReadRow("商品", i));
                    if (p == null || p.CategoryId == string.Empty)
                    {
                        break;
                    }
                    i++;
                    ProductService service = new ProductService();
                    service.AddProduct(p);
                }
                catch {
                    return;
                }
            }
        }

        /// <summary>
        /// 从数据库导出到Excel
        /// </summary>
        public string ExportProductToExcel(string categoryId)
        {
            try
            {

                ExcelHelper<ProductForExcel> excelHelper = new ExcelHelper<ProductForExcel>(GlobalHelper.GetValueByKey("ExcelTemplate") + "商品.xls", GlobalHelper.GetValueByKey("ExcelFile"));
                ProductService service = new ProductService();

                int count = 0;

                IList<ProductForExcel> plist = AutoMapper.Mapper.Map<IList<Product>, IList<ProductForExcel>>(service.FetchAllByCategory(categoryId, 1, 10, out count));

                int pageCount = count % 10 == 0 ? count / 10 : count / 10 + 1;

                int rowIndex = 4;

                for (int pageIndex = 1; pageIndex <= pageCount; pageIndex++)
                {
                    for (int i=0; i < plist.Count; i++)
                    {
                        excelHelper.AddRow("商品", plist[i], rowIndex);
                        rowIndex++;
                    }
                    plist = AutoMapper.Mapper.Map<IList<Product>, IList<ProductForExcel>>(service.FetchAllByCategory(categoryId, pageIndex+1, 10, out count));
                }
                return excelHelper.WriteToFile();
            }
            catch (LogicException ee)
            {
                throw ee;
            }
            catch (Exception ee)
            {
                LogHelper.WriteLog(ee.Message);
                throw new LogicException("系统异常,请联系管理员");
            }
        }
    }
}
